package cc.lj.internal.coupons.infrastructure.repository.redis;

import com.alibaba.nacos.common.JustForTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author liangxifeng
 * @date 2023/6/12 16:00
 */
@Repository
public class RedisDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 测试String类型
     */
    public void testString()
    {
        // 存入永久数据
        redisTemplate.opsForValue().set("test2", "1");
        // 也可以向redis里存入数据和设置缓存时间
        redisTemplate.opsForValue().set("test1", "hello redis", 1000, TimeUnit.SECONDS);
    }
    /**
     * Hash结构测试 set
     */
    public void testHashSet()
    {
        //put 单条数据
        redisTemplate.opsForHash().put("hash","hash-key","hash-value");
        redisTemplate.opsForHash().put("hash","hash-key2","hash-value2");

        //put 多条数据
        Map<String, String> map = new HashMap<>(16);
        map.put("hash-key3", "value3");
        map.put("hash-key4", "value4");
        redisTemplate.opsForHash().putAll("hash", map);

        //如果变量值存在，在变量中可以添加不存在的的键值对，如果变量不存在，则新增一个变量，同时将键值对添加到该变量。添加成功返回true否则返回false
        Boolean absent = redisTemplate.opsForHash().putIfAbsent("hash", "hash-key", "value1");
        Boolean absent2 = redisTemplate.opsForHash().putIfAbsent("hash", "hash-key5", "value5");
        System.out.println(absent);
        System.out.println(absent2);
    }
    /**
     * Hash结构测试 get
     */
    public void testHashGet()
    {
        //获取指定指定key的所有值 结果：[hash-value, hash-value2, value3, value4]
        List<Object> values = redisTemplate.opsForHash().values("hash");
        System.out.println(values.toString());

        //获取变量中的键值对。结果：{hash-key=hash-value, hash-key2=hash-value2, hash-key3=value3, hash-key4=value4}
        Map<Object, Object> entries = redisTemplate.opsForHash().entries("hash");
        System.out.println(entries.toString());

        //获取变量中的指定map键是否有值,如果存在该map键则获取值，没有则返回null。结果：hash-value
        Object value = redisTemplate.opsForHash().get("hash", "hash-key");
        System.out.println(value);

        //以集合的方式获取变量中的值。结果：[hash-value, hash-value2]
        List<Object> values2 = redisTemplate.opsForHash().multiGet("hash", Arrays.asList("hash-key", "hash-key2"));
        System.out.println(values2.toString());

        //匹配获取键值对，ScanOptions.NONE为获取全部键对，
        // ScanOptions.scanOptions().match("hash-key2").build()匹配获取键位map1的键值对,不能模糊匹配。
        //结果：hash-key---->hash-value
        //hash-key2---->hash-value2
        //hash-key3---->value3
        //hash-key4---->value4
        Cursor<Map.Entry<Object, Object>> scan = redisTemplate.opsForHash().scan("hash", ScanOptions.NONE);
        while (scan.hasNext()) {
            Map.Entry<Object, Object> next = scan.next();
            System.out.println(next.getKey() + "---->" + next.getValue());
        }

    }

    /**
     * 模糊批次key
     */
    public void testKeyPattern() {
        long res = redisTemplate.opsForHash().increment("demoHashKey","1000",1);
        System.out.println(res);
        /*
        //取出batch_为前缀的所有key的名称
        Set<String> keySet = redisTemplate.keys("global_use_shiyongma_*");
        System.out.println(keySet);
        if(keySet.isEmpty())
        {
            System.out.println("不存在模糊批次的key");
        }*/

    }
}
